var v = mlog.variable;
var uploadsfolder = '/uploads';
var defaultAvatar = v.base + '/images/default_avatar.png';

var m = {};
$.extend(m, {
	/**
	 * 获取用户的SpaceUrl
	 */
	spaceUrl : function(name){
		return v.base + '/u/' + name;
	}, 
	
	/**
	 * 获取用户的小图像
	 */
	smallAvatar : function(smallAvatar){
		if(!smallAvatar){
			return defaultAvatar;
		}
		return v.base + uploadsfolder + smallAvatar;
	},
	
	/**
	 * 获取未读消息
	 * @param showId 显示消息数量的容器编号
	 */
	getMessageCount : function(callback){
		$.post(v.base + '/message/count', function(response){
			if(response.success == true){
				callback(response.data.count);
			}
		});
	},
});


/*************************************/
//以下为通用方法
/*************************************/
/**
 * 将按钮状态修改成加载中，防止重复提交表单
 * 
 * @param btn
 *            按钮
 * @param txt
 *            按钮替换文本
 */
function changeButtomLoading(btn, txt) {
	if(!txt){
		txt = '提交中...';
	}
	if(btn.tagName == 'A'){
		$(btn).addClass('button-disabled');
		$(btn).text(txt);
	}
	else if(btn.tagName == 'INPUT'){
		$(btn).attr('disabled', 'disabled');
		$(btn).val(txt);
	}
}



/*************************************/
//以下为扩展JQuery的方法
/*************************************/
$.fn.extend({
	ctrlEnter : function(fn, thisObj) {
		var obj = thisObj || this;
		var stat = false;
		return this.each(function() {
			$(this).keyup(function(event) {
				if (event.keyCode == 17) {
					stat = true;
					setTimeout(function() {
						stat = false;
					}, 300);
				}
				if (event.keyCode == 13 && (stat || event.ctrlKey)) {
					fn.call(this, event);
				}
			});
		});
	}
});

$.fn.setCursorPosition = function(position){
    if(this.lengh == 0) return this;
    return $(this).setSelection(position, position);
}
$.fn.setSelection = function(selectionStart, selectionEnd) {
    if(this.lengh == 0) return this;
    input = this[0];
    if (input.createTextRange) {
        var range = input.createTextRange();
        range.collapse(true);
        range.moveEnd('character', selectionEnd);
        range.moveStart('character', selectionStart);
        range.select();
    } else if (input.setSelectionRange) {
        input.focus();
        input.setSelectionRange(selectionStart, selectionEnd);
    }
    return this;
}
$.fn.focusEnd = function(){
    this.setCursorPosition(this.val().length);
}